package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.InsTacheLog;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 12:12
 * @description
 */
@Repository("insTacheLogArchiverDao")
public class insTacheLogArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_TACHE_LOG WHERE TACHE_INST_ID = ?");
        List<InsTacheLog> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(InsTacheLog.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `REPORTER`, `LOG_TIME`, `PARTITION_CODE`, `TACHE_INST_ID`, `TEMPLATE_ID`, `CONSUME_TIME`, `TACHE_DESC`");
        sql.append(" FROM INS_TACHE_LOG WHERE TACHE_INST_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<InsTacheLog> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsTacheLog insTacheLog = new InsTacheLog();
                insTacheLog.setId(resultSet.getLong("ID"));
                insTacheLog.setTacheInstId(resultSet.getString("TACHE_INST_ID"));
                insTacheLog.setReporter(resultSet.getString("REPORTER"));
                insTacheLog.setLogTime(resultSet.getString("LOG_TIME"));
                insTacheLog.setConsumeTime(resultSet.getLong("CONSUME_TIME"));
                insTacheLog.setTemplateId(resultSet.getString("TEMPLATE_ID"));
                insTacheLog.setTacheDesc(resultSet.getString("TACHE_DESC"));
                insTacheLog.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insTacheLog;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_tache_log`");
        sql.append(" (`ID`, `REPORTER`, `LOG_TIME`, `PARTITION_CODE`, `TACHE_INST_ID`, `TEMPLATE_ID`, `CONSUME_TIME`, `TACHE_DESC`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:reporter,:logTime,:partitionCode,:tacheInstId,:templateId,:consumeTime,:tacheDesc,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {

    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_TACHE_LOG WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
